//
//  ChatTextCell.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2020/2/17.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class ChatRecordCell: ChatBaseCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        bubbleView.addSubview(contentLabel)
        bubbleView.addSubview(logoImgView)
        addTap()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func configure(with vm: ChatMsgViewModel, detailVm: AdvisoryDetailViewModel?) {
        super.configure(with: vm, detailVm: detailVm)
        contentLabel.textColor = vm.userType.secondsTextColor(isPlaying: vm.isPlaying) 
        contentLabel.attributedText = NSAttributedString(string: "\(vm.time)''")

        // 设置泡泡
        bubbleView.image = vm.bubbleImage
        
        logoImgView.image = UIImage(named: "chat_record_list_icon")
        logoImgView.tintColor = vm.audioLogoColor
        
        setupUILayout(with: vm)
    }
    
    lazy var contentLabel: UILabel = {
        let contentL = UILabel()
        contentL.numberOfLines = 0
        contentL.textAlignment = .left
        contentL.font = .regular(16)
        contentL.sizeToFit()
        return contentL
    }()
    
    lazy var logoImgView: UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "chat_record_list_icon"))
        return imgView
    }()
    
    func setupUILayout(with vm: ChatMsgViewModel) {
        guard let vm = viewModel else { return }
        let bubbleViewWidth = 90.0 + Double(vm.time - 1) * 2.5
        // 重新布局
        avatarView.snp.remakeConstraints { (make) in
            make.width.height.equalTo(kChatAvatarWidth)
            make.top.equalTo(sendTimeLabel.snp.bottom).offset(8)
        }
        bubbleView.snp.remakeConstraints { (make) in
            make.top.equalTo(avatarView)
            make.bottom.equalTo(-12)
        }
        logoImgView.snp.remakeConstraints { (make) in
            make.width.height.equalTo(20)
            make.centerY.equalTo(bubbleView.snp.centerY)
        }
        contentLabel.snp.remakeConstraints { (make) in
            make.centerY.equalTo(bubbleView.snp.centerY)
        }
        sendTimeLabel.snp.remakeConstraints { (make) in
            make.top.centerX.equalToSuperview()
        }
        errorView.snp.remakeConstraints { (make) in
            make.width.height.equalTo(16)
            make.centerY.equalTo(bubbleView)
        }
        activityView.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.centerY.equalTo(bubbleView)
        }
        
        switch vm.userType {
        case .patient:
            avatarView.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-kChatMargin)
            }
            bubbleView.snp.makeConstraints { (make) in
                make.right.equalTo(avatarView.snp.left).offset(-kChatMargin)
                make.width.equalTo(bubbleViewWidth)
            }
            errorView.snp.makeConstraints { (make) in
                make.right.equalTo(bubbleView.snp.left).offset(-kChatMargin)
            }
            activityView.snp.makeConstraints { (make) in
                make.right.equalTo(bubbleView.snp.left).offset(-kChatMargin)
            }
            logoImgView.snp.makeConstraints { (make) in
                make.right.equalTo(bubbleView.snp.right).offset(-20)
            }
            contentLabel.snp.makeConstraints { (make) in
                make.right.equalTo(logoImgView.snp.left).offset(-10)
            }
        case .doctor:
            avatarView.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(kChatMargin)
            }
            bubbleView.snp.makeConstraints { (make) in
                make.left.equalTo(avatarView.snp.right).offset(kChatMargin)
                make.width.equalTo(bubbleViewWidth)
            }
            logoImgView.snp.makeConstraints { (make) in
                make.left.equalTo(bubbleView.snp.left).offset(20)
            }
            contentLabel.snp.makeConstraints { (make) in
                make.left.equalTo(logoImgView.snp.right).offset(10)
            }
        }
    }
}

extension ChatRecordCell {
    func addTap() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(tap:)))
        bubbleView.isUserInteractionEnabled = true
        bubbleView.addGestureRecognizer(tap)
    }
    
    @objc func tapAction(tap: UITapGestureRecognizer) {
        delegate?.chatRccordCellPlay(cell: self)
    }
}
